﻿using System;
using System.Globalization;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.EntityModel;
using Resources;

namespace MonkeyWarranty.Web.Products
{
    public partial class EditGroup : System.Web.UI.Page
    {
        #region Properties

        private ProductGroupBiz _productGroupBiz;

        private ProductGroupBiz ProductGroupManager
        {
            get { return _productGroupBiz ?? (_productGroupBiz = new ProductGroupBiz()); }
        }

        private int _groupId;
        private int GroupId
        {
            get
            {
                if (_groupId <= 0)
                    int.TryParse(Request.QueryString["Id"], out _groupId);
                return _groupId;
            }
        }

        private ProductGroup _productGroup;
        private ProductGroup CurrentProductGroup
        {
            get
            {
                if (_productGroup == null && GroupId > 0)
                {
                    _productGroup = ProductGroupManager.GetById(GroupId);
                }
                return _productGroup;
            }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Load data
                if (CurrentProductGroup == null)
                {
                    lblErrorMessage.Text = lblErrorMessage.Text = string.Format("{0}: Id={1}", GlobalStrings.NotExisted, GroupId);
                    btnSave.Enabled = false;
                }
                else
                {
                    txtCode.Text = CurrentProductGroup.Code;
                    txtName.Text = CurrentProductGroup.Name;
                    txtDescription.Text = CurrentProductGroup.Description;
                    txtSortOrder.Text = CurrentProductGroup.SortOrder.ToString(CultureInfo.InvariantCulture);
                    chkHidden.Checked = CurrentProductGroup.Hidden;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;
            errorMessage.Visible = false;

            // validate inputs
            var code = txtCode.Text.Trim();
            if (code.Length == 0)
            {
                lblErrorMessage.Text = GlobalStrings.CodeIsRequired;
                errorMessage.Visible = true;
                return;
            }
            var name = txtName.Text.Trim();
            if (name.Length == 0)
            {
                lblErrorMessage.Text = GlobalStrings.NameIsRequired;
                errorMessage.Visible = true;
                return;
            }
            var descrition = txtDescription.Text;
            int sortOrder;
            int.TryParse(txtSortOrder.Text.Trim(), out sortOrder);
            var isHidden = chkHidden.Checked;

            CurrentProductGroup.Code = code;
            CurrentProductGroup.Name = name;
            CurrentProductGroup.Description = descrition;
            if (sortOrder > 0)
                CurrentProductGroup.SortOrder = sortOrder;
            CurrentProductGroup.Hidden = isHidden;

            // update
            ProductGroupManager.UpdateGroup(CurrentProductGroup);

            lblErrorMessage.Text = GlobalStrings.UpdateSuccess;
            errorMessage.Visible = true;
        }

        #endregion
    }
}